package com.phantasy.scene
{
	import com.phantasy.common.GlobalObjectManager;
	import com.phantasy.modules.scene.SceneDataManager;
	import com.phantasy.scene.sceneData.MapDataVo;
	import com.phantasy.scene.sceneKit.MouseIcon;
	import com.phantasy.scene.sceneManager.LoopManager;
	import com.phantasy.scene.sceneUnit.IMutualUnit;
	import com.phantasy.scene.sceneUnit.MyRole;
	import com.phantasy.scene.sceneUnit.Role;
	import com.phantasy.scene.sceneUnit.baseUnit.MutualAvatar;
	import com.phantasy.scene.sceneUnit.map.Map;
	import com.phantasy.scene.tile.Pt;
	import com.phantasy.scene.tile.TileUitls;
	import com.phantasy.scene.tile.gameAstar.MCAstar;
	import com.phantasy.scene.tile.gameAstar.MapGrid;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	public class GameScene extends Sprite
	{
		private static var _instance:GameScene;
		private var visibleArray:Dictionary;
		private var mouseIcon:MouseIcon;
		private var tileTestLayer:Sprite;
		private var enterFrameNum:int;
		
		public var map:Map;
		public var signLayer:Sprite;
		public var lowEffLayer:Sprite;
		public var midLayer:Sprite;
		public var highEffLayer:Sprite;
		public var mapGrid:MapGrid;
		public var astar:MCAstar;
		public var hero:MyRole;
		public var heroXED:Number = 0;
		public var heroYED:Number = 0;
		public function GameScene()
		{
			visibleArray = new Dictionary();
			mouseIcon = MouseIcon.getInstance();
			
			super();
			this.addEventListener(Event.ADDED_TO_STAGE, this.onAddedStage);
		}
		
		public static function getInstance():GameScene
		{
			if (_instance == null)
			{
				_instance = new GameScene();
			}
			return _instance;
		}
		
		private function onAddedStage(event:Event):void
		{
			this.removeEventListener(Event.ADDED_TO_STAGE, this.onAddedStage);
			this.parent.addEventListener(MouseEvent.MOUSE_DOWN, this.onSceneMouseDown);
			this.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.onStageMouseDown);
		}
		
		private function onSceneMouseDown(event:MouseEvent):void
		{
			
		}
		
		private function onStageMouseDown(event:MouseEvent):void
		{
			
		}
		
		public function reset():void
		{
			var mapDataVo:MapDataVo = SceneDataManager.mapData;
			if(this.map==null){
				this.map = new Map();
				if(this.map.parent==null){
					this.parent.addChildAt(this.map,0);
				}
			}
			
			if(this.tileTestLayer==null){
				this.tileTestLayer = new Sprite();
				this.tileTestLayer.mouseChildren = this.tileTestLayer.mouseEnabled=false;
				this.addChild(tileTestLayer);
			}
			
			if(this.signLayer==null){
				this.signLayer = new Sprite();
				this.addChild(this.signLayer);
			}
			
			if(this.lowEffLayer==null){
				this.lowEffLayer = new Sprite();
				this.addChild(this.lowEffLayer);
			}
			
			if(this.midLayer==null){
				this.midLayer = new Sprite();
				this.addChild(midLayer);
			}
			
			if(this.highEffLayer==null){
				this.highEffLayer = new Sprite();
				this.addChild(this.highEffLayer);
			}
			
			this.tileTestLayer.x = this.signLayer.x = this.lowEffLayer.x = this.midLayer.x = this.highEffLayer.x = mapDataVo.offsetX;
			this.tileTestLayer.y = this.signLayer.y = this.lowEffLayer.y = this.midLayer.y = this.highEffLayer.y = mapDataVo.offsetY;
			
			this.map.reset();
			
			this.mapGrid = new MapGrid(mapDataVo);
			this.astar = new MCAstar(this.mapGrid);
			if (this.mouseIcon.parent == null)
			{
				this.signLayer.addChild(this.mouseIcon);
			}
			LoopManager.addToFrame(this, this.onSceneEnterFrame);
			
		}
		
		private function onSceneEnterFrame():void
		{
			this.enterFrameNum++;
			if ((this.enterFrameNum % 3) == 0)
			{
				this.map.loadPiece();
			}
		}
		
		override public function set x(_arg1:Number):void
		{
			super.x = _arg1;
			if (this.map)
			{
				this.map.x = _arg1;
			}
		}
		
		override public function set y(_arg1:Number):void
		{
			super.y = _arg1;
			if (this.map)
			{
				this.map.y = _arg1;
			}
		}
		
		/**
		 *添加单元 
		 * @param _arg1
		 * @param _arg2
		 * @param _arg3
		 * @param _arg4
		 * 
		 */		
		public function addUnit(unit:IMutualUnit, _arg2:int, _arg3:int, _arg4:int=4):void
		{
			var _local5:Point = TileUitls.getIsoIndexMidVertex(new Pt(_arg2, 0, _arg3));
			unit.x = _local5.x;
			unit.y = _local5.y;
			if (unit is MutualAvatar)
			{
				(unit as MutualAvatar).turnDir(_arg4);
			}
			
			if(unit is Role){
				
			}else{
				if(unit is DisplayObject){
					this.midLayer.addChild(unit as DisplayObject);
				}
			}
			
		}
		
		public function centerCamera(_arg1:Number, _arg2:Number, _arg3:Boolean=false):void
		{
			var _local4:int;
			var _local5:int;
			var _local6:Number;
			var _local7:Number;
			var _local8:int;
			var _local9:int;
			var _local10:int;
			var _local11:int;
			if (((!((this.heroXED == _arg1))) || (!((this.heroYED == _arg2)))))
			{
				this.heroXED = _arg1;
				this.heroYED = _arg2;
				_arg1 = (_arg1 + SceneDataManager.mapData.offsetX);
				_arg2 = (_arg2 + SceneDataManager.mapData.offsetY);
				_local4 = GlobalObjectManager.GAME_WIDTH;
				_local5 = GlobalObjectManager.GAME_HEIGHT;
				_local6 = (_local4 >> 1);
				_local7 = (_local5 >> 1);
				_local8 = SceneDataManager.mapData.width;
				_local9 = SceneDataManager.mapData.height;
				_local10 = -1;
				_local11 = -1;
				if ((((_arg1 >= _local6)) && ((_arg1 <= (_local8 - _local6)))))
				{
					_local10 = (_local6 - _arg1);
				}
				else
				{
					if (_arg1 < _local6)
					{
						_local10 = 0;
					}
					else
					{
						if (_arg1 > (_local8 - _local6))
						{
							_local10 = (_local4 - _local8);
						};
					};
				};
				if ((((_arg2 >= _local7)) && ((_arg2 <= (_local9 - _local7)))))
				{
					_local11 = (_local7 - _arg2);
				}
				else
				{
					if (_arg2 < _local7)
					{
						_local11 = 0;
					}
					else
					{
						if (_arg2 > (_local9 - _local7))
						{
							_local11 = (_local5 - _local9);
						};
					};
				};
				if ((((GlobalObjectManager.GAME_WIDTH > _local8)) && (!((this.x == 0)))))
				{
					_local10 = 0;
				};
				if ((((GlobalObjectManager.GAME_HEIGHT > _local9)) && (!((this.y == 0)))))
				{
					_local11 = 0;
				};
				if (_arg3)
				{
					if (_local10 != -1)
					{
						this.x = _local10;
					};
				}
				else
				{
//					if (ClimbTowerModule.getInstance().isFxbgMap == true)
//					{
//						if (this.checkIsMove(_arg1, _local10))
//						{
//							if (_local10 != -1)
//							{
//								this.x = _local10;
//							};
//						};
//					}
//					else
//					{
						if (_local10 != -1)
						{
							this.x = _local10;
						};
					//};
				};
				if (_local11 != -1)
				{
//					if (!(((ClimbTowerModule.getInstance().isFxbgMap == true)) && ((GlobalObjectManager.GAME_HEIGHT > 900))))
//					{
//						this.y = _local11;
//					}
				}
			}
		}
		
		/**
		 *清理场景 
		 * 
		 */		
		public function clean():void
		{
			LoopManager.removeFromFrame(this);
			Map.isCanLoadedMap = false;
			if(this.map){
				this.map.clear();
			}
		}

	}
}